<template>
  <div class="app-container">
    <!-- 上部分表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="85px"
      :rules="rules"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col :span="6" v-if="authUser">
          <el-form-item label="油企名称" prop="enterpriseId" size="small">
            <el-select
              style="width: 200px"
              v-model="queryParams.enterpriseId"
              :clearable="true"
              filterable
              placeholder="请选择油企"
              @change="handleSelectChange"
            >
              <el-option
                v-for="(item, index) in enterpriseList"
                :key="index"
                :value="item.enterpriseId"
                :label="item.enterpriseName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="优惠券状态" prop="deliveryStatus" size="small">
            <el-select
              style="width: 200px"
              v-model="queryParams.deliveryStatus"
              :clearable="true"
              filterable
              placeholder="请选择优惠券发放状态"
            >
              <el-option
                v-for="v in deliveryStatusArr"
                :key="v.value"
                :value="v.value"
                :label="v.label"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户手机号" prop="originMobilePhone">
            <el-input
              style="width: 200px"
              v-model="queryParams.originMobilePhone"
              placeholder="请输入用户手机号"
              clearable
              @keyup.enter.native="handleQuery"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label-width="120px"
            label="拉新用户手机号"
            prop="newMobilePhone"
          >
            <el-input
              style="width: 200px"
              v-model="queryParams.newMobilePhone"
              placeholder="请输入拉新用户手机号"
              clearable
              @keyup.enter.native="handleQuery"
              size="small"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="拉新状态" prop="newUserStatus" size="small">
            <el-select
              style="width: 200px"
              v-model="queryParams.newUserStatus"
              placeholder="请选择拉新状态"
              clearable
            >
              <el-option value="0" label="未激活"></el-option>
              <el-option value="1" label="已激活"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="拉新时间" size="small">
            <el-date-picker
              size="small"
              v-model="dateRangeCreatedDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 200px"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row
      class="global_el_rowStyle"
      :gutter="10"
      style="
        border-top: none;
        border-bottom: 1px solid #f5f5f5;
        margin-top: 0px;
      "
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          @click="resetQuery"
          size="mini"
          icon="el-icon-refresh"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          plain
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table border style="width: 100%" :data="dataList" v-loading="loading">
      <af-table-column width="100" type="index" label="序号" align="left" />
      <af-table-column width="180" label="油企" align="left" prop="enterpriseName"  v-if="authUser"/>
      <af-table-column
        label="用户名称"
        align="left" width="180"
        prop="originUserNickName"
      />
      <af-table-column
        label="用户手机号"
        align="left" width="180"
        prop="originMobilePhone"
      />
      <af-table-column
        label="拉新用户名称"
        align="left" width="180"
        prop="newUserNickName"
      />
      <af-table-column
        label="拉新用户手机号"
        align="left" width="180"
        prop="newMobilePhone"
      />
      <af-table-column label="拉新状态" align="left" prop="newUserStatus" width="180">
        <template slot-scope="{ row }">
          {{ row.newUserStatus === 0? '未激活': '已激活' }}
        </template>
      </af-table-column>
      <af-table-column label="拉新时间" align="left" prop="createTime" width="180"/>
      <af-table-column label="优惠券发放" align="left" prop="deliveryStatus" >
        <template slot-scope="{ row }">
          {{ deliveryStatusArr[row.deliveryStatus] && deliveryStatusArr[row.deliveryStatus].label }}
        </template>
      </af-table-column>
    </el-table>
    <!-- </div> -->
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { cloneDeep } from "lodash";
import { enterpriseList } from "@/api/station/order";
import { listEnterpriseStation, getStationByEnterprise } from "@/api/dept";
import getUserProfileName from "@/utils/getUserProfile";
import { exportList, getDataList } from "@/api/customer/recommendList";
export default {
  name: "recommendList",
  data() {
    return {
      // 优惠劵发放状态
      deliveryStatusArr: [
        { value: "0", label: "未发放" },
        { value: "1", label: "已发放" },
        { value: "2", label: "发放失败" },
        { value: "3", label: "部分发放" },
      ],
      // 支付时间
      paymentTime: [],
      //创建订单时间间隔
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 订单支付表格数据
      dataList: [],
      query: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {
        beginTime: null,
        endTime: null,
        enterpriseId: null,
        newMobilePhone: null,
        newUserStatus: null,
        deliveryStatus: null,
        originMobilePhone: null,
      },
      // 表单校验
      rules: {
        originMobilePhone: [
          {
            pattern: /^\d{1,20}$/,
            message: "请输入小于20位数字",
            trigger: "blur",
          },
        ],
      },
      stationList: [],
      enterpriseList: [],
      loading: false,
    };
  },
  created() {
    this.getList();
    this.getEnterpriseList();
    // 如果当前是油企账号，则获取该企业下的油站
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.getStationList(this.presentEnterprise.enterpriseId);
    } else {
      this.getStationList();
    }
  },
  computed: {
    authUser() {
      let roles = this.$store.state.user.roles;
      if (roles.includes("admin")) {
        return true;
      } else {
        return false;
      }
    },
  },
  methods: {
    // 根据油企获取油站
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },
    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },

    /** 查询订单支付列表 */
    async getList() {
      this.loading = true;
      if(this.dateRangeCreatedDate) {
        this.queryParams.beginTime = this.dateRangeCreatedDate[0]
        this.queryParams.endTime = this.dateRangeCreatedDate[1]
      }
      const res = await getDataList(this.query,this.queryParams);
      this.total = res.total;
      this.dataList = res.rows;
      this.loading = false;
    },

    async getStationList(val) {
      // 判断是不是admin账号
      const isDelete = await getUserProfileName();
      if (isDelete) {
        listEnterpriseStation({ enterpriseId: val }).then((res) => {
          this.stationList = res;
        });
      } else {
        const res = await getStationByEnterprise();
        this.stationList = res.data;
      }
    },
    /** 搜索按钮操作 */
    async handleQuery() {
      this.query.pageNum = 1;
      await this.$refs["queryForm"].validate();
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        beginTime: null,
        endTime: null,
        enterpriseId: null,
        newMobilePhone: null,
        newUserStatus: null,
        deliveryStatus: null,
        originMobilePhone: null,
      }
      this.dateRangeCreatedDate = []
      this.handleQuery();
    },

    /** 导出按钮操作 */
    async handleExport() {
        this.queryParams.beginTime = this.dateRangeCreatedDate[0],
        this.queryParams.endTime = this.dateRangeCreatedDate[1],
        await this.$confirm("是否确认导出列表数据项?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        const response = await exportList(this.queryParams);
        this.download(response.msg);
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-bottom: 10px;
}
.amt_sty {
  ::v-deep .el-input__inner {
    width: 95%;
  }
}
.button-from {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .button-style1 {
    display: inline-block;
    width: 80px;

    .el-button {
      margin: 0 0 0 15px;
    }
  }
}

::v-deep .card-table {
  padding: 10px;
  margin-top: 8px;
  background-color: #f8f8f8;

  .el-button {
    margin: 0 5px 0 0;
  }
}

::v-deep .el-table {
  border: 1px #f8f8f8 solid;
  border-bottom: 1px #dfe6ec solid;
}

::v-deep .is-center {
  border: 1px transparent solid;
  border-bottom: 1px #f8f8f8 solid;
}

</style>
